using Abp.Web.Security.AntiForgery;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Http;

namespace Abp.AspNetCore.Security.AntiForgery;

public class AbpAspNetCoreAntiForgeryManager : IAbpAntiForgeryManager
{
    public IAbpAntiForgeryConfiguration Configuration { get; }

    private readonly IAntiforgery _antiforgery;
    private readonly IHttpContextAccessor _httpContextAccessor;

    public AbpAspNetCoreAntiForgeryManager(
        IAntiforgery antiforgery,
        IHttpContextAccessor httpContextAccessor,
        IAbpAntiForgeryConfiguration configuration)
    {
        Configuration = configuration;
        _antiforgery = antiforgery;
        _httpContextAccessor = httpContextAccessor;
    }

    public string GenerateToken()
    {
        return _antiforgery.GetAndStoreTokens(_httpContextAccessor.HttpContext).RequestToken;
    }
}